package com.example.reflectiondemo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @program my-project-model
 * @description:
 * @author: lee
 * @create: 2023/01/04 19:52
 */
public class ReflectMain {

    private String readOnly;

    public String getReadOnly() {
        return readOnly;
    }

    public void setReadOnly(String readOnly) {
        System.out.println("set");
        this.readOnly = readOnly;
    }


    //    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
//        ReflectMain t = new ReflectMain();
//        Field f = t.getClass().getDeclaredField("readOnly");
//        f.setAccessible(true);
//        f.set(t, "test");
//        System.out.println(t.getReadOnly());
//
//    }
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ReflectMain t = new ReflectMain();
        Method setReadOnly = t.getClass().getMethod("setReadOnly", String.class);
        String s = "test2";
        setReadOnly.invoke(t, s);
        System.out.println(t.getReadOnly());

    }

}
